package com.dpj.netty.c3.chat.server.handler;

import com.dpj.netty.c3.chat.message.GroupCreateRequestMessage;
import com.dpj.netty.c3.chat.message.GroupCreateResponseMessage;
import com.dpj.netty.c3.chat.server.session.Group;
import com.dpj.netty.c3.chat.server.session.GroupSession;
import com.dpj.netty.c3.chat.server.session.GroupSessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.List;
import java.util.Set;

@ChannelHandler.Sharable
public class GroupCreateRequestMessageHandler extends SimpleChannelInboundHandler<GroupCreateRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, GroupCreateRequestMessage groupCreateRequestMessage) throws Exception {
        String groupName = groupCreateRequestMessage.getGroupName();
        Set<String> members = groupCreateRequestMessage.getMembers();
        //群管理器
        GroupSession groupSession = GroupSessionFactory.getGroupSession();
        Group group = groupSession.createGroup(groupName, members);
        if(group==null){
            //发送成功消息
            channelHandlerContext.writeAndFlush(new GroupCreateResponseMessage(true,groupName+"创建成功"));
            //发送拉群消息
            List<Channel> channels = groupSession.getMembersChannel(groupName);
            for (Channel channel : channels) {
                channel.writeAndFlush(new GroupCreateResponseMessage(true,"您已被拉入"+groupName));


            }



        }else {
             channelHandlerContext.writeAndFlush(new GroupCreateResponseMessage(false,groupName+"创建失败"));

        }
    }
}
